
/**
 * process 当返回true时阻止
 * built {type,target}
 *      [string] type 内置动作类型（MOVE）
 *      [array] target 响应的节点
 *          [
 *              { dom:可原生, style:{left: 初始属性, top: parseInt(this.style.top) } },
 *              { dom:box1, style:{left: parseInt(box1.style.left), top: parseInt(box1.style.top) } }
 *          ]
 * ending
 */
(function(W, D, $){

    D.CAN_MOVE = false;
    D.MOVE_RANG = [function () {},function () {}];
    var X, Y;

    $(D).on('mousemove touchmove', function (e) {
        if (D.CAN_MOVE) {
            X = e.pageX || e.originalEvent.targetTouches[0].pageX;
            Y = e.pageY || e.originalEvent.targetTouches[0].pageY;
            D.MOVE_RANG[0](X, Y);
        }
    }).on('mouseup touchend', function (e) {
        if (D.CAN_MOVE) {
            D.MOVE_RANG[1](X, Y); X=null; Y=null;
            D.CAN_MOVE = false;
            D.MOVE_RANG = [function () {},function () {}];
        }
    });

    /**
     * 全局滑动管理
     * swipe.start(option) swipe.x(option,'x') swipe.y(option,'y')
     *     option:{
     *          process: function
     *          end: function
     *          built: object:{
     *              type: 'MOVE'
     *              target: array:[
     *                  { dom, style:{left: parseInt(this.style.left), top: parseInt(this.style.top) } }
     *              ]
     *          }
     *     }
     * @type {swipe}
     */
    W.swipe = new function () {
        var swipe = this,
            timer = new function () {
                var ts, te;
                this.start = function () {
                    ts = new Date().getTime()
                };
                this.end = function () {
                    te = new Date().getTime();
                    return te - ts;
                };
            };
        var pageX, pageY;


        swipe.start = function (e,option) {

            pageX = e.pageX || e.originalEvent.targetTouches[0].pageX;
            pageY = e.pageY || e.originalEvent.targetTouches[0].pageY;
            timer.start();    //记录开始
            D.CAN_MOVE = true;
            if (option.start) {
                option.start(e, pageX, pageY);
            }
            D.MOVE_RANG = [function (x, y) {
                if (option.process) {
                    option.process(x-pageX, y-pageY);
                }
            }, function (x, y) {

                if (option.end) {
                    var offX= x==null?0:x-pageX;
                    var offY= y==null?0:y-pageY;
                    option.end(offX, offY, timer.end())
                }
            }];

        };
        swipe.end= function(){

        };
    };


}(window,document,jQuery));